import axios from 'axios'
import { Message, Notification as Notify } from 'element-ui'
import { allowRequest, stopRepeatRequest } from './clearHttp'
import { getCach } from './cach'
import MD5 from 'md5'
import Vue from 'vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const CancelToken = axios.CancelToken;
// import qs from 'qs'

// 正在进行中的请求列表
Vue.$httpRequestList = [];

const http = axios.create({
	timeout: 300000
	// transformRequest: [data => {
	// 	return qs.parse(data);
	// }],
})

http.interceptors.request.use(config => {
	if (config._loading) {
		NProgress.start()
	}
	return config;
}, error => {
	Promise.resolve(error);
	NProgress.done()
})

http.interceptors.response.use(response => {
	NProgress.done()
	const { _msg, api_id } = response.config;
	allowRequest(api_id)
	if (response.request.responseURL.indexOf('export') !== -1 || response.request.responseURL.indexOf('import') !== -1 || response.request.responseURL.indexOf('image') !== -1) {
		return response.data
	}
	if (!response.data.status) {
		switch (response.data.code) {
			// case 401:
			// 	process.env.NODE_ENV=='production'?window.location.href = response.data.data:'';
			// 	break;
			case -404:
				return Notify.error({ title: '错误',
                              dangerouslyUseHTMLString: true,
                              message: `<span style="word-break:break-all">${response.data.message || '未知异常'}</span>` })
			case -500:
				return Notify.error({ title: '错误',
                              dangerouslyUseHTMLString: true,
                              message: `<span style="word-break:break-all">${response.data.message || '服务器连接错误！'}</span>` })
			case response.request.responseURL.indexOf('exportExcel' || 'importExcel') !== -1:
				break;
			default:
				Promise.reject(response.data.message || '未知异常');
				return Notify.error({ title: '错误',
                              dangerouslyUseHTMLString: true,
                              message: `<span style="word-break:break-all">${response.data.message || '服务器连接错误！'}</span>` })
		}
	}

	if (_msg) {
		Message.success({
			showClose: true,
			message: response.data.message || _msg,
			type: 'success'
		})
	}
	return response.data
}, error => {
	if (!axios.isCancel(error)) {
		Promise.reject(error);
		Notify.error({ title: '错误', message: '服务器开小差啦！' })
		allowRequest(error.config.api_id)
	}
	NProgress.done()
});

export default {
	$GET(url, params = {}, config = {}) {
		params.timer = new Date().getTime() // 规避IE缓存
		const api_id = MD5(JSON.stringify({ url, params, methods: 'GET' }));
		stopRepeatRequest(api_id)
		return http.get(url, {
			params,
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		})
	},

	$POST(url, params, config) {
		const api_id = MD5(JSON.stringify({ url, params, methods: 'POST' }));
		stopRepeatRequest(api_id)
		return http.post(url, params, {
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		},
		)
	},

	$DELETE(url, params, config) {
		const api_id = MD5(JSON.stringify({ url, params, methods: 'DELETE' }));
		stopRepeatRequest(api_id)
		return http.delete(url, {
			data: params, ...config, api_id, cancelToken: new CancelToken(function executor(c) {
				setClearFnName({ url, params, methods: 'DELETE' }, c)
			})
		})
	},

	$PUT(url, params, config) {
		const api_id = MD5(JSON.stringify({ url, params, methods: 'PUT' }));
		stopRepeatRequest(api_id)
		return http.put(url, params, {
			...config,
			api_id,
			cancelToken: new CancelToken(function executor(c) {
				setClearFnName(api_id, c)
			})
		})
	},

	$ALL() {
		return axios.all(arguments)
	}
}


function setClearFnName(api_id, c) {
	let clearFn = {};
	clearFn[api_id] = c;
	Vue.$httpRequestList.push(clearFn)
}
